#!/bin/sh
USERS=
_UID=0
   
function add_user {
    while true
      do
         echo -n "Enter name:"
         read name
         echo -n "Enter gender(male|fema1):"
         read gender
         echo -n "Enter age:"
         read age

         user_info="$name:$gender:$age"
         USERS[$_UID]=user_info
         _UID=$(($_UID+1))
         echo "There are ${#USERS[@]} users"
         echo -n "Do you want to add more(y/n):"
         read answer
         if [ $answer == "n" -o $answer == "n" ];then
              break
         fi
    done
}


function query_user {
  echo "请输入姓名"

  read name
  name1=$name
  for x in ${USERS[@]}
  do
    Y=`echo ${x}|awk -F":"'{print $1}'`
    if [ $Y == "$name1"];then
      echo ${x}
     break;
    else
        echo "没有此学员的信息"
    fi
done
}
function update_user {
	echo "请输入需要跟新的学员”
	read name
	name1=$name
	for x in ${USERS[@]}
	do
		echo $x
		name2=`echo ${x} | awk -F":" '{print $1 }'`
		if [ $name2 == "$name1" ];then
			name3="${x}"
			echo -e "输入根改信息"
			echo "name:gender:age"
			read name4
			break;
		else
			name5=$(($name2+1))
		fi
		done
		USERS[$name5]="name4"
}


while true;
do
     echo "1.添加学员"
     echo "2.查询学员"
     echo "3.更新学员"
     echo "4.退出系统"

     echo -n "请选择"
     read choice

     if [ $choice -eq 1 ];then
        add_user
     elif [ $choice -eq 2 ];then
        query_user
     elif [ $choice -eq 3 ];then
        update_user
     elif [ $choice -eq 4 ];then
        exit 0
     else
         echo "错误的选择！！！"
     fi
done

